package com.jyw.business.de.domain.dto.web;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.jyw.business.de.domain.DeDemandInfo;
import com.jyw.business.de.domain.DeDemandSub;
import com.jyw.common.annotation.Excel;
import com.jyw.common.utils.bean.BeanUtils;
import com.jyw.system.domain.vo.SimpleFileVo;
import com.jyw.system.utils.FileUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.util.CollectionUtils;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Data
@ApiModel("新增需求请求实体")
public class DemandInfoAddDto {

    /** 需求标题 */
    @ApiModelProperty("需求标题")
    @NotEmpty(message = "需求标题不能为空")
    private String demandTitle;

    /** 需求描述 */
    @ApiModelProperty("需求描述")
    private String demandRemark;

    /** 涉及系统(数据字典多选) */
    @ApiModelProperty("涉及系统(数据字典多选)")
    private List<String> demandSystem;

    /** 附件 */
    @ApiModelProperty("附件")
    private List<SimpleFileVo> demandEnclosure;

    /** 提出日期 */
    @ApiModelProperty("提出日期")
    @JsonFormat(pattern = "yyyy-MM-dd")
    @NotNull(message = "提出日期不能为空")
    private Date submitDate;

    @ApiModelProperty("提出科室集合")
    private List<DemandSubDto> subInfo;

    /** 需求描述 */
    @ApiModelProperty(value = "需求类型",hidden = true)
    private String demandType;

    @ApiModelProperty(value = "需求来源",hidden = true)
    private String demandSource;

    @ApiModelProperty("是否提交")
    private boolean submit;

    public DeDemandInfo buildDemand() {
        DeDemandInfo deDemandInfo = new DeDemandInfo();
        BeanUtils.copyProperties(this,deDemandInfo);
        if(!CollectionUtils.isEmpty(getDemandEnclosure())){
            deDemandInfo.setDemandEnclosure(FileUtils.fileToId(getDemandEnclosure()));
        }
        if(!CollectionUtils.isEmpty(getDemandSystem())){
            deDemandInfo.setDemandSystem(getDemandSystem().stream().collect(Collectors.joining(",")));
        }
        deDemandInfo.setDemandProcess(0);
        return deDemandInfo;
    }

    public List<DeDemandSub> buildSub(DeDemandInfo info) {
        return getSubInfo().stream().map(data -> {
            DeDemandSub sub = new DeDemandSub();
            BeanUtils.copyProperties(data, sub);
            sub.setDemandId(info.getId());
            return sub;
        }).collect(Collectors.toList());
    }
}
